package me.zhengjie.modules.biz.util;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayResponse;

import java.math.BigDecimal;

/** JSAPI 下单为例 */
public class QuickStart {

//  /** 商户号 */
//  public static String merchantId = "";
//  /** 商户API私钥路径 */
//  public static String privateKeyPath = "";
//  /** 商户证书序列号 */
//  public static String merchantSerialNumber = "";
//  /** 商户APIV3密钥 */
//  public static String apiV3Key = "";

  /** 商户号 */
  public static String merchantId = "1666873139";
  /** 商户API私钥路径 */
  public static String privateKeyPath = "-----BEGIN PRIVATE KEY-----\n" +
      "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDNLCLp7dza6PQN\n" +
      "DOREX0HsGzcUeGm1kFAosPQWhx7ANUj60NEwScpFxeGHY2NJNyMUvzO7R1ZRmRUg\n" +
      "QbpTczn14TOpLJb3iNB9nb4Q5eqUtkPf0HhhAbTzvUb1iO7xLWfPaG8PH1E+DRmc\n" +
      "VKJJrQnqfQoPd2WJAp0qLnkiYeMwXtvGmCYtRFcBs0UUAKKo8IJH687Gx/XsXsIS\n" +
      "aWRPjCKSfrozvLPVBEmUnZ/FMrRKD38XqurCzb8njQAnoGKDUxg7JBtUAuQgdxGa\n" +
      "MPqI6nk85BhFtYzzJmRIT5X0qBTSa7EktsABX3fGWCSMULUqJ1AvksP34se6brak\n" +
      "55VO6E1tAgMBAAECggEBAJ+JglyEKQfPvHWpYJYJvCjrwJiaSRh1bt81OZeK2c57\n" +
      "oJPna8IwnqhChmvQTJPR2+V2B7PIdMZeUp8SfOAG6TrYMmOJ2JoCOJx03CYnoZCh\n" +
      "aYXPhMtpmFsvAs5J/AJtXBtbCYCoyXOGnDODW+4OL3e2YuxD2x/+eFcka+zgUNh6\n" +
      "hSmgF7yfHXfBIo4ciO/MEP5/warX1CRXzAbg0TTjXPpKE8qzTAPnK2D+/eJwrDgT\n" +
      "eLlq+zumeUg4QKZ4hLc7vkPYuZCN/YRiE0g6ybSgvM3sdmEaAMS2n1oXrvvzQzdI\n" +
      "ppJehlK8FgmEuZGPe5bevkyWgdeojOSAQV6uqIbKYgECgYEA9CAjkaHFk9mFq4NY\n" +
      "pYBdivgp/apXsCD++tKWzMj6HBUp5bhhiADwYaMIdDtj0qgfpg0lCh8/F2XI6tV5\n" +
      "6o5die3X9yzYGvCprgvNm68eYSPKNGXbqCMZLYhKJvHy3OXfGAigW4YAVyF4hYgI\n" +
      "WH5+eQNsq0XpJS5qW6M4Ykn8+IkCgYEA1ybzkZZkWRsA28yfMULVCCM5fzv6myx9\n" +
      "Wuh4DAsWt9AA7SIuxP2JQny+P4uA1580pXRw4DQlf2oRM07PCveb/RAtDGUuF00P\n" +
      "vdkRYGGASqlpR+AXIaoTn6NfKzwV6nfc575DHmMV8MqtSjAK41mfkjuOmBoCxmjo\n" +
      "HR8uuJP5rMUCgYBiZV3QwgJvwDn3Ul51fpK1g6bxHipvzCzZje3IJq1giksz0yhE\n" +
      "eBRrVhn/NzzZ2HfBJwUp2kREuvrLZDXai306LcphTwrfeOb7Rridqmy1PIawrelm\n" +
      "EJeCWa+BmP2JeYF9H2T63rzeYZhBQf4TJRD216GCOxBosxedTeFZBHdY+QKBgGD1\n" +
      "9ejTrQ+XItDrq4aIxvbu4PedMTiDWU8B2zAaQze9bt1vfUJbvGMRUq/Sj9MctZ4n\n" +
      "FsOD3K5KP8wupRIdOthWVOb0Jkxr9z8otzMHXcjIBmFU9Te/rS5Rcw9J+dp8ADsU\n" +
      "PK1SHE4zGNuJQS7qcgZ1/rI/bqa6GnkdV3Hphii9AoGBAJlYlIsJCnksvkmB6/yS\n" +
      "4Qjl9bIxRQR+/awJnA9FE8mlzoPAbIHGZ+UGBYX3anmuHsaRS+D1t8AUEIl/3c0s\n" +
      "+RAbvriI0KuCQCZ0bWmUpDB7NQ+mgaj9lj2pBDB1RsaUxflVHQUXrsHhwp8KSbXI\n" +
      "gkgS6o41NZ8MAxmDtsMlvCII\n" +
      "-----END PRIVATE KEY-----\n";
  /** 商户证书序列号 */
  public static String merchantSerialNumber = "4B6345CDCAF1ADE521E5EA5E27438530A4C6A1C2";
  /** 商户APIV3密钥 */
  public static String apiV3Key = "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq";

    public static void main(String[] args) {
      double price = 10.50; // 你的价格
      BigDecimal priceBigDecimal = BigDecimal.valueOf(price);
      BigDecimal priceInCents = priceBigDecimal.multiply(BigDecimal.valueOf(100));

      // 如果需要四舍五入到最接近的整数
      BigDecimal roundedPriceInCents = priceInCents.setScale(0, BigDecimal.ROUND_HALF_UP);

      System.out.println("Price in cents: " + roundedPriceInCents);
    }

//  public static void main(String[] args) {
//    // 使用自动更新平台证书的RSA配置
//    // 一个商户号只能初始化一个配置，否则会因为重复的下载任务报错
//    Config config =
//        new RSAAutoCertificateConfig.Builder()
//            .merchantId(merchantId)
//            .privateKey(privateKeyPath)
//            .merchantSerialNumber(merchantSerialNumber)
//            .apiV3Key(apiV3Key)
//            .build();
//    JsapiService service = new JsapiService.Builder().config(config).build();
//    // request.setXxx(val)设置所需参数，具体参数可见Request定义
//    PrepayRequest request = new PrepayRequest();
//    Amount amount = new Amount();
//    // 1分
//    amount.setTotal(1);
//    request.setAmount(amount);
//    request.setAppid("wx268d5785a8f076dc");
//    request.setMchid(merchantId);
//    request.setDescription("测试商品");
//    request.setNotifyUrl("https://localhost:8000/notify_url");
//    request.setOutTradeNo("out_trade_no_003");
//    Payer payer = new Payer();
//    // openId 用户openId
//    payer.setOpenid("oHR-p6z9a3vY55oxTcLto-GYXCVY");
//    request.setPayer(payer);
//    PrepayResponse response = service.prepay(request);
//    System.out.println(response.getPrepayId());
//  }
}
